package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-02
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Api(tags = "讲师管理")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation("删除讲师")
    @DeleteMapping("removeTeacher/{id}")
    public R removeTeacher(@PathVariable(name = "id") String id) {
        boolean b = eduTeacherService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("分页查询讲师")
    @GetMapping("pageTeacher")
    public R pageTeacher(@RequestParam(value = "pageNum") Long pageNum,@RequestParam(value = "pageSize") Long pageSize) {
        // 创建page对象 mybatis-plus中的page
        Page<EduTeacher> teacherPage = new Page<>(pageNum,pageSize);

        // 底层会把所有数据封装到teacherPage中
        eduTeacherService.page(teacherPage,null);
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("分页条件查询")
    @GetMapping("pageQueryTeacher")
    public R pageQueryTeacher(@RequestParam(value = "pageNum") Long pageNum, @RequestParam(value = "pageSize") Long pageSize, TeacherQuery teacherQuery) {
        // 创建分页对象
        Page<EduTeacher> teacherPage = new Page<>(pageNum,pageSize);
        // 构建查询条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 获取传递过来的 条件参数
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        // 判断条件是否为空 类似于动态sql StringUtils 是spring中的包 column 数据库中的字段 后边传入的值


        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }

        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }



        // 调用service方法
        eduTeacherService.page(teacherPage,wrapper);
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacher")
    public R getTeacher(@RequestParam String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("rows",teacher);
    }

    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("修改讲师")
    @PutMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }
    }
}

